package com.ruoyi.system.service.impl;

import com.ruoyi.common.config.oss.OssProperties;
import com.ruoyi.common.config.oss.OssTemplate;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PersonCharge;
import com.ruoyi.system.mapper.PersonChargeMapper;
import com.ruoyi.system.service.IPersonChargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class PersonChargeServiceImpl implements IPersonChargeService {

    @Autowired
    private OssTemplate template;

    @Autowired
    private OssProperties ossProperties;

    @Autowired
    private PersonChargeMapper personChargeMapper;

    @Override
    public void create(PersonCharge personCharge){
        personCharge.setChargeId(SecurityUtils.getUserId().intValue());
        personCharge.setChargeName(SecurityUtils.getUsername());
        personCharge.setUpdateName(SecurityUtils.getUsername());
        personChargeMapper.create(personCharge);
    }

    @Override
    public List<PersonCharge> list(PersonCharge personCharge) {
        return personChargeMapper.list(personCharge);
    }

    @Override
    public void delete(Integer id) {
        PersonCharge personCharge = personChargeMapper.getById(id);
        String fileName = personCharge.getFileName();
        if (StringUtils.isNotNull(fileName)) {
            try {
                template.removeObject(ossProperties.getBucketName(),fileName);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        personChargeMapper.delete(id);
    }

    @Override
    public void update(PersonCharge personCharge) {
        personCharge.setUpdateName(SecurityUtils.getUsername());
        personChargeMapper.update(personCharge);
    }

    @Override
    public void deleteFile(Integer id) {
        String username = SecurityUtils.getUsername();
        personChargeMapper.updateFileColumn(id,username);
    }
}
